package com.meteor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.meteor.api.feign.system.SystemFeign;
import com.meteor.api.feign.system.dto.SysMerchantDTO;
import com.meteor.convert.RoomConvert;
import com.meteor.convert.UnitConvert;
import com.meteor.entity.Room;
import com.meteor.entity.Unit;
import com.meteor.framework.common.base.ResponseData;
import com.meteor.framework.common.constant.Constant;
import com.meteor.framework.common.enums.UserAdminEnum;
import com.meteor.framework.common.exception.ServerException;
import com.meteor.framework.common.utils.PageResult;
import com.meteor.framework.mybatis.service.impl.BaseServiceImpl;
import com.meteor.framework.security.user.SecurityUser;
import com.meteor.mapper.RoomMapper;
import com.meteor.mapper.UnitMapper;
import com.meteor.query.OrgAndCommunityQuery;
import com.meteor.query.RoomQuery;
import com.meteor.query.UnitQuery;
import com.meteor.service.RoomService;
import com.meteor.service.UnitService;
import com.meteor.vo.RoomVO;
import com.meteor.vo.UnitVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lynn
 * @date 2024/1/22
 */


@Service
@AllArgsConstructor
public class RoomServiceImpl extends BaseServiceImpl<RoomMapper, Room> implements RoomService {


    private final SystemFeign systemFeign;


    @Override
    public void save(RoomVO vo) {
        Room  entity= RoomConvert.INSTANCE.convert(vo);

        if(!SecurityUser.getUser().getUsername().equalsIgnoreCase(UserAdminEnum.ADMIN.getValue())){

            if(ObjectUtils.isEmpty(vo.getOrgId())){
                entity.setOrgId(SecurityUser.getUser().getOrgId());
            }
        }else{
            if(ObjectUtils.isEmpty(vo.getOrgId())) {
                throw new ServerException("请选择机构");
            }
            ResponseData responseData = JSON.parseObject(systemFeign.orgMerchant(vo.getOrgId()),ResponseData.class);
            if(!ObjectUtils.isEmpty(responseData.getData())){
                SysMerchantDTO sysMerchantDTO = JSON.parseObject(responseData.getData().toString(),SysMerchantDTO.class);
                entity.setMerchantId(sysMerchantDTO.getId());
            }
            entity.setOrgId(vo.getOrgId());
            /*if(ObjectUtils.isEmpty(vo.getMerchantId())) {
                throw new ServerException("请选择商户");
            }*/
        }

//        entity.setMerchantId(SecurityUser.getUser().getMerchantId());
//        entity.setOrgId(SecurityUser.getUser().getOrgId());

        baseMapper.insert(entity);
    }

    @Override
    public void update(RoomVO vo) {
        Room room = RoomConvert.INSTANCE.convert(vo);
        if (ObjectUtils.isEmpty(room.getId())) {
            throw new ServerException("修改失败，id为空");
        }
        baseMapper.updateById(room);

    }

    @Override
    public RoomVO get(String id) {
        return RoomConvert.INSTANCE.convert(baseMapper.selectById(id));
    }

    @Override
    public List<RoomVO> getList(RoomQuery query) {

        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(query.getRoomName()), Room::getRoomName, query.getRoomName());
        wrapper.eq(StrUtil.isNotBlank(query.getRoomCode()), Room::getRoomCode, query.getRoomCode());
        wrapper.eq(StrUtil.isNotBlank(query.getCommunityId()), Room::getCommunityId, query.getCommunityId());
        wrapper.eq(StrUtil.isNotBlank(query.getBuildingId()), Room::getBuildingId, query.getBuildingId());
        wrapper.eq(StrUtil.isNotBlank(query.getUnitId()), Room::getUnitId, query.getUnitId());
        // 数据权限
        dataScopeWrapper(wrapper);
        List<Room> list = baseMapper.selectList(wrapper);

        return RoomConvert.INSTANCE.convertList(list);
    }

    @Override
    public PageResult<RoomVO> page(RoomQuery query) {

        // 查询参数
        Map<String, Object> params = getParams(query);

        // 分页查询
        IPage<Room> page = getPage(query);
        params.put(Constant.PAGE, page);

        // 数据列表
        List<RoomVO> list = baseMapper.getList(params);

        return new PageResult<>(list, page.getTotal());

//        IPage<Complaint> page = baseMapper.selectPage(getPage(query), getWrapper(query));
//
//        return new PageResult<ComplaintVO>(ComplaintConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    @Override
    public void delete(String id) {
        removeById(id);
    }

    @Override
    public List<RoomVO> unitRoom(OrgAndCommunityQuery query) {
        LambdaUpdateWrapper<Room> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(query.getOrgId()),Room::getOrgId, query.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(query.getUnitId()),Room::getUnitId, query.getUnitId());
        wrapper.eq(Room::getDeleted,0);
        return RoomConvert.INSTANCE.convertList(list(wrapper));
    }

    private Map<String, Object> getParams(RoomQuery query) {
        Map<String, Object> params = new HashMap<>();
        params.put("roomName", query.getRoomName());
        params.put("roomCode", query.getRoomCode());
        params.put("communityId", query.getCommunityId());
        params.put("buildingId", query.getBuildingId());
        params.put("unitId", query.getUnitId());

        // 数据权限
        params.put(Constant.DATA_SCOPE, getDataScope("t1", null,"pf_room"));

        return params;
    }

    private Wrapper<Room> getWrapper(RoomQuery query){
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(query.getRoomName()), Room::getRoomName, query.getRoomName());
        wrapper.eq(StrUtil.isNotBlank(query.getRoomCode()), Room::getRoomCode, query.getRoomCode());
        wrapper.eq(StrUtil.isNotBlank(query.getCommunityId()), Room::getCommunityId, query.getCommunityId());
        wrapper.eq(StrUtil.isNotBlank(query.getBuildingId()), Room::getBuildingId, query.getBuildingId());
        wrapper.eq(StrUtil.isNotBlank(query.getUnitId()), Room::getUnitId, query.getUnitId());

        // 数据权限
        dataScopeWrapper(wrapper);

        return wrapper;
    }
}
